package com.zds.scm.dao.impl;


import java.io.Serializable;
import java.sql.ResultSet;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.UserInfoDAO;
import com.zds.scm.domain.UserInfo;

public class UserInfoDAOImpl extends AbstractDAOImpl<UserInfo> implements UserInfoDAO {

	@Override
	protected UserInfo rowMapper(ResultSet rs){
		UserInfo userInfo=new UserInfo();
		try {
			userInfo.setUserCode(rs.getString("user_Code"));
			userInfo.setUserName(rs.getString("user_Name"));
			userInfo.setUserPwd(rs.getString("user_Pwd"));
			userInfo.setUserType(rs.getString("user_Type"));
			return userInfo;
		} catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
			logger.error("DAO层结果集映射处理错误:"+e.getMessage());
			throw new DaoException(e.getMessage(),e);
		}
	}
	@Override
	public boolean insert(UserInfo userInfo) {
		String sql="insert into(user_code,user_name,user_pwd,user_type) UserInfo values(?,?,?,?)";
		Object[] args=new Object[]{userInfo.getUserCode(),userInfo.getUserName(),userInfo.getUserPwd(),userInfo.getUserType()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean delete(UserInfo userInfo){
		String sql="delete from UserInfo where User_Code=?";
		Object[] args=new Object[]{userInfo.getUserCode()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean update(UserInfo userInfo) {
		String sql="update UserInfo set user_Name=?,user_Pwd=?,user_Type=? where user_Code=?";
		Object[] args=new Object[]{userInfo.getUserName(),userInfo.getUserPwd(),userInfo.getUserType(),userInfo.getUserCode()};
		return super.updateEntity(sql, args)>0;
	}
	@Override
    public UserInfo findById(Serializable id) {
    	String sql="select * from UserInfo where user_Code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}
	@Override
	public List<UserInfo> findAll() {
		return findAll(-1, -1);
	}
	@Override
	public List<UserInfo> findAll(int currentPage, int pageSize) {
		String   sql ="select * from UserInfo";
		Object[] args=null;
		if(currentPage<1||pageSize<1){//不分页
			args=new Object[]{};
		}else {   //分页
			sql+=" limit ?,?";
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRows() {
		String sql="select count(*) from userinfo";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<UserInfo> findByCode(String likeUserCode, int currentPage,int pageSize) {
		String sql="select * from UserInfo where user_name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){//不分页
			args=new Object[]{"%"+likeUserCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeUserCode+"%",(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByCode(String likeUserCode) {
		String sql="select count(*) from userinfo where user_code like ?";
		Object[] args=new Object[]{"%"+likeUserCode+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<UserInfo> findByName(String likeUserName,int currentPage, int pageSize) {
		String sql="select * from UserInfo where user_name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){//不分页
			args=new Object[]{"%"+likeUserName+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeUserName+"%",(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByName(String likeUserName) {
		String sql="select count(*) from userinfo where user_name like ?";
		Object[] args=new Object[]{"%"+likeUserName+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<UserInfo> findByType(String userType, int currentPage,int pageSize) {
		String sql="select * from UserInfo where user_type=?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){//不分页
			args=new Object[]{userType};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{userType,(currentPage-1)*pageSize,pageSize};
		}
		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByType(String userType) {
		String sql="select count(*) from userinfo where user_type=?";
		Object[] args=new Object[]{userType};
		return super.findEntityCount(sql, args);
	}
}
